import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

class NoDataWidget extends StatelessWidget {
  /// 图标
  final IconData? icon;

  /// 图标颜色
  final Color? iconColor;

  /// 图标大小
  final double? iconSize;

  /// 标题
  final String? title;

  /// 标题样式
  final TextStyle? titleStyle;

  /// 描述文字
  final String? description;

  /// 描述文字样式
  final TextStyle? descriptionStyle;

  /// 按钮文字
  final String? buttonText;

  /// 按钮点击事件
  final VoidCallback? onButtonTap;

  /// 按钮颜色
  final Color? buttonColor;

  /// 是否显示按钮
  final bool showButton;

  /// 背景颜色
  final Color? backgroundColor;

  /// 内边距
  final EdgeInsetsGeometry? padding;

  /// 图标和文字之间的间距
  final double? iconTextSpacing;

  /// 文字和按钮之间的间距
  final double? textButtonSpacing;

  const NoDataWidget({
    Key? key,
    this.icon,
    this.iconColor,
    this.iconSize,
    this.title,
    this.titleStyle,
    this.description,
    this.descriptionStyle,
    this.buttonText,
    this.onButtonTap,
    this.buttonColor,
    this.showButton = false,
    this.backgroundColor,
    this.padding,
    this.iconTextSpacing,
    this.textButtonSpacing,
  }) : super(key: key);

  /// 默认的空列表样式
  factory NoDataWidget.emptyList({
    Key? key,
    String? title,
    String? description,
    String? buttonText,
    VoidCallback? onButtonTap,
    bool showButton = false,
  }) {
    return NoDataWidget(
      key: key,
      icon: Icons.inbox_outlined,
      iconColor: ColorUtils.c_cccccc,
      iconSize: 64.r,
      title: title ?? 'No Data',
      description: description ?? 'There is no data to display at the moment.',
      buttonText: buttonText,
      onButtonTap: onButtonTap,
      showButton: showButton,
      buttonColor: ColorUtils.themeColor,
    );
  }

  /// 默认的空搜索结果样式
  factory NoDataWidget.noSearchResult({
    Key? key,
    String? title,
    String? description,
    String? buttonText,
    VoidCallback? onButtonTap,
    bool showButton = false,
  }) {
    return NoDataWidget(
      key: key,
      icon: Icons.search_off_rounded,
      iconColor: ColorUtils.c_cccccc,
      iconSize: 64.r,
      title: title ?? 'No Search Results',
      description: description ?? 'No items found matching your search criteria.',
      buttonText: buttonText,
      onButtonTap: onButtonTap,
      showButton: showButton,
      buttonColor: ColorUtils.themeColor,
    );
  }

  /// 默认的网络错误样式
  factory NoDataWidget.networkError({
    Key? key,
    String? title,
    String? description,
    String? buttonText,
    VoidCallback? onButtonTap,
    bool showButton = true,
  }) {
    return NoDataWidget(
      key: key,
      icon: Icons.wifi_off_rounded,
      iconColor: ColorUtils.c_fd5155,
      iconSize: 64.r,
      title: title ?? 'Network Error',
      description: description ?? 'Please check your internet connection and try again.',
      buttonText: buttonText ?? 'Retry',
      onButtonTap: onButtonTap,
      showButton: showButton,
      buttonColor: ColorUtils.themeColor,
    );
  }

  /// 默认的加载失败样式
  factory NoDataWidget.loadFailed({
    Key? key,
    String? title,
    String? description,
    String? buttonText,
    VoidCallback? onButtonTap,
    bool showButton = true,
  }) {
    return NoDataWidget(
      key: key,
      icon: Icons.error_outline_rounded,
      iconColor: ColorUtils.c_fd5155,
      iconSize: 64.r,
      title: title ?? 'Load Failed',
      description: description ?? 'Failed to load data. Please try again.',
      buttonText: buttonText ?? 'Retry',
      onButtonTap: onButtonTap,
      showButton: showButton,
      buttonColor: ColorUtils.themeColor,
    );
  }

  /// 默认的收藏为空样式
  factory NoDataWidget.emptyFavorites({
    Key? key,
    String? title,
    String? description,
    String? buttonText,
    VoidCallback? onButtonTap,
    bool showButton = true,
  }) {
    return NoDataWidget(
      key: key,
      icon: Icons.favorite_border_rounded,
      iconColor: ColorUtils.c_fd5155,
      iconSize: 64.r,
      title: title ?? 'No Favorites',
      description: description ?? 'You haven\'t added any items to your favorites yet.',
      buttonText: buttonText ?? 'Start Shopping',
      onButtonTap: onButtonTap,
      showButton: showButton,
      buttonColor: ColorUtils.themeColor,
    );
  }

  /// 默认的购物车为空样式
  factory NoDataWidget.emptyCart({
    Key? key,
    String? title,
    String? description,
    String? buttonText,
    VoidCallback? onButtonTap,
    bool showButton = true,
  }) {
    return NoDataWidget(
      key: key,
      icon: Icons.shopping_cart_outlined,
      iconColor: ColorUtils.c_cccccc,
      iconSize: 64.r,
      title: title ?? 'Empty Cart',
      description: description ?? 'Your shopping cart is empty. Add some items to get started.',
      buttonText: buttonText ?? 'Start Shopping',
      onButtonTap: onButtonTap,
      showButton: showButton,
      buttonColor: ColorUtils.themeColor,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: padding ?? EdgeInsets.all(32.r),
      color: backgroundColor ?? Colors.transparent,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 图标
          if (icon != null) ...[
            Container(
              width: (iconSize ?? 64.r) + 20.r,
              height: (iconSize ?? 64.r) + 20.r,
              decoration: BoxDecoration(
                color: (iconColor ?? ColorUtils.c_cccccc).withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular((iconSize ?? 64.r) + 10.r),
              ),
              child: Icon(
                icon,
                color: iconColor ?? ColorUtils.c_cccccc,
                size: iconSize ?? 64.r,
              ),
            ),
            SizedBox(height: iconTextSpacing ?? 24.r),
          ],

          // 标题
          if (title != null) ...[
            Text(
              title!,
              style: titleStyle ??
                  StyleUtils.ts_1f_16_600.copyWith(
                    color: ColorUtils.c_666666,
                    fontSize: 18.r,
                  ),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 8.r),
          ],

          // 描述文字
          if (description != null) ...[
            Text(
              description!,
              style: descriptionStyle ?? StyleUtils.ts_99_14_400,
              textAlign: TextAlign.center,
              maxLines: 3,
              overflow: TextOverflow.ellipsis,
            ),
            SizedBox(height: textButtonSpacing ?? 24.r),
          ],

          // 按钮
          if (showButton && buttonText != null) ...[
            Container(
              width: double.infinity,
              height: 44.r,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [
                    buttonColor ?? ColorUtils.themeColor,
                    (buttonColor ?? ColorUtils.themeColor).withValues(alpha: 0.8),
                  ],
                ),
                borderRadius: BorderRadius.circular(12.r),
                boxShadow: [
                  BoxShadow(
                    color: (buttonColor ?? ColorUtils.themeColor).withValues(alpha: 0.3),
                    blurRadius: 8.r,
                    offset: Offset(0, 2.r),
                  ),
                ],
              ),
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  borderRadius: BorderRadius.circular(12.r),
                  onTap: onButtonTap,
                  child: Center(
                    child: Text(
                      buttonText!,
                      style: StyleUtils.ts_ff_14_500.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ],
      ),
    );
  }
}
